package dao;

import java.util.LinkedList;
import java.util.List;

import relations.belongsTo;
import entities.Album;
import entities.Order;
import entities.User;

public class belongsToDAO {

	private static final belongsToDAO instance = new belongsToDAO();
	private List<User> belongsTosUser;
	private List<Order>belongsTosOrder;
	
	private belongsToDAO(){
		belongsTosUser = new LinkedList<User>();
		belongsTosOrder = new LinkedList<Order>();
		belongsTo cus = null;
		
		cus = belongsTo.newInstance("jaime", "Jaime", "Vazquez", "ja@j.com", 1L, "test", "kl", "1", "00778", "PR", "Gurabo", "USA");
		belongsTos.add(cus);
		cus = belongsTo.newInstance("jaime1", "Jaime", "Vazquez", "ja@j.com", 2L, "test", "kl", "1", "00778", "PR", "Gurabo", "USA");
		belongsTos.add(cus);
	}
	
	public static belongsToDAO getInstance() {
		return instance;
	}

	public synchronized belongsTo findbelongsTo(String UserID, String orderID) {
		Order result1 = null;
		belongsTo result = null;
		
		for (Order s : belongsTosOrder){
			if (s.getOrderID().equals(orderID) && s.getUserID().equals(UserID)){
				result1 = s;
			}
		}
		result = new belongsTo(result1.getUserID(), result1.getOrderID());
	
		return result;
	}

	public synchronized void addbelongsTo(User objUser, Order objOrder) throws RecordDuplicateException{
		if (findbelongsTo(objUser.getUserID(), objOrder.getOrderID() ) != null){
			throw new RecordDuplicateException("Relation belongsTo with UserId: " + objUser.getUserID() + " and" + " OrderId: " + objUser.getUserID() + " is already in the db.");
		}
		else {
			belongsTosUser.add(objUser);
			belongsTosOrder.add(objOrder);
		}
	}

}
